package logic;

import java.util.ArrayList;

import javax.swing.JLabel;

import gui.*;

public class Debugger implements Runnable{

	private final int ROW = 0;
	private final int COL = 1;
	
	private boolean 			_Running;
	private Pestanna			_CurrentPestanna;
	private int					_CurrentX;
	private int 				_CurrentY;
	private Block				_PreviousBlock;
	private int 				_PreviousX;
	private int 				_PreviousY;
	private ArrayList<Pestanna>	_Pestannas;
	private BoardPanel			_BoardPanel;
	private SemanticAnalizer 	_Analizador;
	private Board 				_Board;
	private int 				_Flujo = 5;
	private int					_MAX = 9;
	private int 				_MIN = 0;
	
	public Board get_Board() {
		return _Board;
	}

	public void set_Board(Board _Board) {
		this._Board = _Board;
	}

	@Override
	public void run() {
		_CurrentPestanna = _Pestannas.get(0);
		_PreviousX = 0;
		_PreviousY = 0;
		_CurrentX = 1;
		_CurrentY = 0;
		while (_Running){
			try {
				Thread.sleep(Constantes.DELAY_DEBUGGER);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			JLabel comp = (JLabel)_BoardPanel.getComponentAt(_CurrentX*50+25, _CurrentY*50+25 );
			
		}
	}
	
	
	public void recorre( int row , int col ){
		int futuroX;
		int futuroY;
		
		Block bloque = _CurrentPestanna.get_Cuadricula()[row][col];
		if ( bloque.getType() ==  Constantes.TYPE_PIPE ){
			if ( ((Pipe)bloque).get_Id() == Constantes.ID_WE ){
				if ( _CurrentPestanna.exist(row-1,col) && _CurrentPestanna.get_Cuadricula()[row-1][col].getType() == Constantes.TYPE_VAR ){
					int value =  ((Var)_CurrentPestanna.get_Cuadricula()[row-1][col]).get_Valor();
					if ( _Flujo + value > _MAX ){
						((Var)_CurrentPestanna.get_Cuadricula()[row-1][col]).set_Valor( _Flujo - _MAX );
						_Flujo = _MAX;
					}else{
						_Flujo += value;
						((Var)_CurrentPestanna.get_Cuadricula()[row-1][col]).set_Valor( _MIN );
					}
				}else if ( _CurrentPestanna.exist(row+1,col) && _CurrentPestanna.get_Cuadricula()[row+1][col].getType() == Constantes.TYPE_VAR ){
					int value =  ((Var)_CurrentPestanna.get_Cuadricula()[row-1][col]).get_Valor();
					if ( _Flujo + value > _MAX ){
						((Var)_CurrentPestanna.get_Cuadricula()[row-1][col]).set_Valor( _MAX );
						_Flujo -= _MAX - value ;
					}else{
						((Var)_CurrentPestanna.get_Cuadricula()[row-1][col]).set_Valor( value + _Flujo );
						_Flujo = _MIN;
					}
				}
			}
		}
		
	}
	
}
